﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Sispe.Entities;

namespace Sispe.Repository
{
    class RegimenLaboralRepository: MasterRepository, IRegimenLaboralRepository
    {
        public List<REGIMEN_LABORAL> GetAllFromRegimenLaboral()
        {
            var query = from p in context.REGIMEN_LABORAL
                        select p;

            return query.ToList();
        }

        public List<REGIMEN_LABORAL> GetAllFromRegimenLaboralByDescripcion(string criterio)
        {
            if (criterio != string.Empty)
            {
                var query = from p in context.REGIMEN_LABORAL
                            where p.Descripcion.ToUpper().Contains(criterio.ToUpper())
                            select p;

                return query.ToList();
            }
            else
            {
                var query = from p in context.REGIMEN_LABORAL
                            select p;

                return query.ToList();
            }
        }

        public REGIMEN_LABORAL GetFromRegimenLaboralById(int id)
        {
            var query = from p in context.REGIMEN_LABORAL
                        where p.IdRegimenLaboral.Equals(id)
                        select p;
            return query.SingleOrDefault();
        }

        public void AddRegimenLaboral(REGIMEN_LABORAL regLab)
        {
            context.REGIMEN_LABORAL.Add(regLab);
            context.SaveChanges();
        }

        public void UpdateRegimenLaboral(REGIMEN_LABORAL regLab)
        {
            context.Entry(regLab).State = System.Data.EntityState.Modified;
            context.SaveChanges();
        }

        public void DeleteRegimenLaboral(int id)
        {
            var existe = context.REGIMEN_LABORAL.Where(p => p.IdRegimenLaboral.Equals(id)).SingleOrDefault();
            if (existe != null)
            {
                context.REGIMEN_LABORAL.Remove(existe);
                context.SaveChanges();
            }
        }

        public bool existeDuplicidad(string descripcion, string codigo, Int32 id)
        {
            var query = from p in context.REGIMEN_LABORAL
                        where (p.Descripcion.ToUpper().CompareTo(descripcion.ToUpper()) == 0
                        || p.Codigo.ToUpper().CompareTo(codigo.ToUpper())==0)
                        && p.IdRegimenLaboral != id
                        select p;

            return query.Any();
        }


        public REGIMEN_LABORAL GetFromRegimenLaboralByCodigo(string codigo)
        {
            var query = from ds in context.REGIMEN_LABORAL
                        where ds.Codigo.Equals(codigo)
                        select ds;
            return query.SingleOrDefault();
        }
    }
}
